<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
class Index extends Controller
{
    public function index()
    {
        $art=model('Arts')->getArt15(0);
       
        $my=model('Site');
        $top=$my->where('sid',1)->select();
        $import=$my->where('sid',2)->select();
        $myy=model('Sector');
        $sectorname=$myy->where('fathersector',3)->select();
        $datas=[];
        foreach ($sectorname as $key => $value) {
            $sid=$value->toArray()['sid'];
            $keyss=$my->where('sid',$sid)->select();
            array_push($datas,$keyss);
        }
        
        return view('index',
        [
            'top'=>$top,
            'import'=>$import,
            'sectorname'=>$sectorname,
            'datas'=>$datas,
            'art'=>$art
        ]);
       
    }

    public function down()
    {
        $my=model('Site');
        $top=$my->where('sid',1)->select();
        $rid=request()->get('rid');
        $art=model('Arts')->where('rid',$rid)->find();
        return view('down',['top'=>$top,'art'=>$art]);
    }

    public function more()
    {
        
        $my=model('Site');
        $top=$my->where('sid',1)->select();
        
        $my=model('Arts');
        $res=$my->alias('a')->join('__SECTOR__ b ','b.sid= a.sid')->order('top desc,rid desc')->paginate(18,false,['path'=>'/down/more.html']);
 
        return view('more',['top'=>$top,'res'=>$res]);

    }

    public function login()
    {
        // dump(request()->post());die;
        $va=validate('UserVa');
        if($va->check(request()->post())){
            $res=model('User')->where('user_name',request()->post()['username'])
                              ->where('password',sha1(request()->post()['password']))
                              ->find();
            
            if($res){
                
                Session::set('user_name',$res['user_name']);
                Session::set('user_id',$res['id']);
                $this->success('登陆成功','/');
            }else{
                $this->error('用户名密码不正确','/');
            }
        }else{
            $this->error($va->getError(),'/');
        }
    }

    public function del()
    {
        Session::clear();
        $this->success('您已退出登陆','/');
    }



}
